package com.mytest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/movie/{id:\\d+}")
    public Movie showMovieInfo(@PathVariable Integer id) {
        /*String url = "http://127.0.0.1:9090/movie/"+id;
        return restTemplate.getForObject(url,Movie.class);*/

        ServiceInstance serviceInstance = loadBalancerClient.choose("MOVIE-SERVICE-PROVIDER");
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/movie/"+id;
        return restTemplate.getForObject(url,Movie.class);
    }

    /*@GetMapping("/movie/{id:\\d+}")
    public Movie findById(@PathVariable Integer id) {

        return new Movie(1001,"无间道II","Andy Liu");
    }*/
}
